package com.icbc.dccsh.bolt;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.joda.time.DateTime;

import com.icbc.dccsh.util.Constants;
import com.icbc.dccsh.util.Watcher;

import backtype.storm.task.TopologyContext;
import backtype.storm.topology.BasicOutputCollector;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseBasicBolt;
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Tuple;
import backtype.storm.tuple.Values;

public class HashBolt extends BaseBasicBolt {
	private flexjson.JSONDeserializer _d = null;
	private Watcher _watcher = null;
	private DateFormat _df2date = null;
	
	@Override
    public void prepare(Map stormConf, TopologyContext context) {
		this._d = new flexjson.JSONDeserializer();
		this._watcher = Watcher.getWatcher(this.getClass());
		this._df2date = new SimpleDateFormat("yyyyMMdd");
    }
	public void execute(Tuple tuple, BasicOutputCollector collector) {
		String s = tuple.getString(0);
		this._watcher.count();
		HashMap<String, Object> map = (HashMap<String, Object>)_d.deserialize(s);
//		map.put("calcdatetime", ZonedDateTime.now().date);
		
		long calcdatetime = System.currentTimeMillis();
		map.put("calcdatetime", calcdatetime);
		
		Date calcDate = new Date(calcdatetime);
		String indexName = this._df2date.format(calcDate);
		map.put("indexname", indexName);
		collector.emit(new Values(map));
		
	}
	public void declareOutputFields(OutputFieldsDeclarer declarer) {
		declarer.declare(new Fields("hash"));
	}
	
	@Override
    public void cleanup() {
    }  
	
}